package club.banyuan;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

@WebServlet(name = "Servlet1", urlPatterns = "/s1", initParams = {
        @WebInitParam(name = "user", value = "zhangsan"),
        @WebInitParam(name = "role", value = "admin")
})
public class Servlet1 extends HttpServlet {

    @Override
    public void init() throws ServletException {

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println("username: " + username);
        System.out.println("password: " + password);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method = request.getMethod();
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String queryString = request.getQueryString();
        String url = request.getRequestURL().toString();
        String uri = request.getRequestURI();
        String servletPath = request.getServletPath();
        String contextPath = request.getContextPath();
        String userAgent = request.getHeader("User-Agent");
        String Connection = request.getHeader("Connection");

        response.getWriter().println("method: " + method);
        response.getWriter().println("username: " + username);
        response.getWriter().println("password: " + password);
        response.getWriter().println("query String: " + queryString);
        response.getWriter().println("url: " + url);
        response.getWriter().println("uri: " + uri);
        response.getWriter().println("servletPath: " + servletPath);
        response.getWriter().println("contextPath: " + contextPath);
        response.getWriter().println("userAgent: " + userAgent);
        response.getWriter().println("Connection: " + Connection);
//        response.sendRedirect(request.getContextPath() + "/test.html");


//        ServletConfig servletConfig = getServletConfig();
//        String user = servletConfig.getInitParameter("user");
//        String role = servletConfig.getInitParameter("role");
//        String username = servletConfig.getInitParameter("username");
//        String password = servletConfig.getInitParameter("password");
//        System.out.println("user= " + user);
//        System.out.println("role= " + role);
//        System.out.println("username= " + username);
//        System.out.println("password= " + password);
//
//        ServletContext servletContext = servletConfig.getServletContext();
////        servletContext.getInitParameter();
//        Enumeration<String> initParameterNames = servletContext.getInitParameterNames();
//        if (initParameterNames.hasMoreElements()) {
//            String s = initParameterNames.nextElement();
//            System.out.println(s + servletContext.getInitParameter(s));
//        }
//
//        Cookie cookie = new Cookie("test","cookieOne");
//        cookie.setMaxAge(0);
//        response.addCookie(cookie);
//        request.getSession();
    }
}
